<?php

namespace App\Http\Controllers\Mobile;

use App\Http\Helpers\Crypter;
use App\Http\Helpers\Sign;
use App\Http\Services\LoginService;
use App\Http\Services\MsgService;
use App\Http\Services\QuestionService;
use App\Http\Services\SchoolService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Validation\Rules\In;

class LoginController extends Controller
{
    /**
     *微校移动端登录
     * @author: lican<364548233@qq.com>
     */
    public function login(Request $request){
        $wxcode = trim($request->input('wxcode'));
        $schoolCode = trim($request->input('state')) ? trim($request->input('state')) : '1013957946';
        $feedbackId = trim($request->input('feedbackId')) ?? ''; //兼容从消息通知点进来的
        $user = LoginService::mobileLogin($wxcode, $schoolCode);
        if($user['code'] == 0 && isset($user['ocode'])){
            $user['school'] = SchoolService::getSchoolInfo($user['ocode']);
            $user['id'] = $user['stu_id'];
            $userId = $user['name']."(".$user['card_number'].")"."[".$user['school']['name']."]"."|".$user['school']['code'];
            $crypter = new Crypter();
            $data = [
                'userid' => $userId,
                'ip' => Sign::getClientIp(),
//            'hardware' => Sign::getPhoneModel(),
                'os' => php_uname('s').php_uname('r'),
                't' => time()
            ];
            $decrypt = [
//            'color' => "08c27c",

            ];

            $url = $crypter::getAuthorizedUrl(BASE_URL, $data, $decrypt);

            header("location:{$url}");
        }else{
            return MsgService::getMsg(1001);
        }
    }

    public function verifyLogin(Request $request){
        $user = LoginService::getUser();
        return MsgService::getMsg(0, ['user' => $user]);
    }

}
